package com.rongzhe.house.house.service;

import com.rongzhe.house.house.criteria.HouseCriteria;
import com.rongzhe.house.house.dao.*;
import com.rongzhe.house.house.entity.*;
import com.rongzhe.house.house.enums.UserCollectionType;
import com.rongzhe.house.house.request.DelegationApplyRequest;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by hggxg on 2017/11/18.
 */
@Slf4j
@Service
public class HouseServiceImpl implements HouseService {


    @Autowired
    private HouseBaseMapper houseBaseMapper;

    @Autowired
    private HouseRoomLiveInfoMapper houseRoomLiveInfoMapper;


    @Autowired
    private HouseRentPaymentConfigMapper houseRentPaymentConfigMapper;

    @Autowired
    private HouseImageMapper houseImageMapper;

    @Autowired
    private HouseTrafficMapper houseTrafficMapper;


    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Autowired
    private HouseDelegationApplyMapper houseDelegationApplyMapper;

    @Autowired
    private LifeServiceMapper lifeServiceMapper;

    @Override
    public List<HouseOverview> getExcellentHouse(String cityId) {

        return houseBaseMapper.selectExcellentHouse(cityId);

    }

    @Override
    public List<HouseOverview> queryHouseByCriteria(HouseCriteria houseCriteria) {


        int total = houseBaseMapper.countHouseByCriteria(houseCriteria);


        if (total == 0) {
            return null;
        }

        int offset = houseCriteria.getPageSize() * (houseCriteria.getCurrentPage() - 1);

        int limit = houseCriteria.getPageSize();

        houseCriteria.setOffset(offset);
        houseCriteria.setLimit(limit);

        List<HouseOverview> houseOverviews = houseBaseMapper.selectHouseByCriteria(houseCriteria);


        return houseOverviews;
    }

    @Override
    public HouseDetail detail(Integer houseId, Integer userId) {

        HouseDetail houseDetail = houseBaseMapper.selectDetailById(houseId);

        if (houseDetail != null) {
            houseDetail.setHouseRoomLiveInfoList(houseRoomLiveInfoMapper.selectRoomInfoListByHouseId(houseId));

            houseDetail.setHouseImageList(houseImageMapper.selectListByHouseId(houseId));

            houseDetail.setHouseTrafficList(houseTrafficMapper.selectListByHouseId(houseId));

        }

        if (userId != null) {
            UserCollection userCollection = userCollectionMapper
                    .selectByUidAndTypeAndBizIdForUpdate(userId, UserCollectionType.HOUSE.getType(), houseId);

            houseDetail.setCanCollection(userCollection == null);
        }


        return houseDetail;
    }

    @Override
    public List<HouseRentPaymentConfig> getHouseRentPayConfigListByHouseId(Integer houseId) {


        return houseRentPaymentConfigMapper.selectHouseRentPayConfigListByHouseId(houseId);

    }

    @Override
    public boolean collection(Integer houseId, Integer userId) {


        UserCollection userCollectionSelect = userCollectionMapper
                .selectByUidAndTypeAndBizIdForUpdate(userId, UserCollectionType.HOUSE.getType(), houseId);


        if (userCollectionSelect == null) {
            //没有收藏
            UserCollection userCollection = new UserCollection();
            userCollection.setUid(userId);
            userCollection.setBizId(houseId);
            userCollection.setInsertUid(userId);
            userCollection.setType((byte) UserCollectionType.HOUSE.getType());
            userCollectionMapper.insertSelective(userCollection);
        } else {
            //已经收藏了，取消收藏
            userCollectionMapper.deleteByPrimaryKey(userCollectionSelect.getId());
        }


        return false;
    }

    @Override
    public List<HouseOverview> collectionGet(Integer userId, Integer pageSize, Integer currentPage) {


        int total = houseBaseMapper.countCollectionHouseByUid(userId);


        if (total == 0) {
            return null;
        }

        int offset = pageSize * (currentPage - 1);

        int limit = pageSize;

        List<HouseOverview> houseOverviews = houseBaseMapper.selectCollectionHouseByUid(userId, offset, limit);


        return houseOverviews;

    }

    @Override
    public boolean delegationApply(HouseDelegationApply delegationApply) {


        int result = houseDelegationApplyMapper.insertSelective(delegationApply);

        return result == 1;
    }

    @Override
    public List<LifeService> getLifeService(Integer type) {

        return lifeServiceMapper.selectByType(type);

    }
}
